/**
 * Created By: sisopipo Project Team
 * Created Date: Feb 25, 2011
 */
package com.sisopipo.assetsportal.security.impl;

import java.util.List;

import com.sisopipo.assetsportal.security.IOrganizationManager;
import com.sisopipo.assetsportal.security.domain.User;
import com.sisopipo.core.model.hibernate.BaseHibernateDAO;

/**
 * @author ribin
 * @version 1.0
 */
@SuppressWarnings("unchecked")
public class OrganizationManagerImpl extends BaseHibernateDAO implements IOrganizationManager {

	public User getUserByPK(String id) {
		return (User) getSession().get(User.class, id);
	}

	public User getUser(String userId) {
		return (User) first(findByHQL("from User where userId = ?", userId));
	}

	public void saveOrUpdateUser(User user) {
		if (user.getId() == null || user.getId().trim().length() == 0)
			user.setId(null);

		// TODO validate User ID unique

		getSession().saveOrUpdate(user);
	}

	public List<User> getAllUsers() {
		return findByHQL("from User order by userId");
	}

	public void removeUser(User user) {
		user = (User) getSession().get(User.class, user.getId());
		if (user != null)
			getSession().delete(user);
	}

}
